#define _CRT_SECURE_NO_WARNINGS 1
#include"List.h"

 LTNode* ListInit()
{
	 LTNode* plist = (LTNode*)malloc(sizeof(LTNode));
	 plist->data = -1;
	 plist->next = plist;
	 plist->prev = plist;
	 return plist;
}


void ListPrintf(LTNode* plist)
{
	assert(plist->next);
	LTNode* plistnext = plist->next;

	while (plistnext != plist)
	{
		printf("%d->", plistnext->data);
		plistnext = plistnext->next;


	}
}


LTNode* ListBuyNode(LTDatatype x)
{
	LTNode* newNode = (LTNode*)malloc( sizeof(LTNode));
	
	newNode->data = x;
	newNode->next = NULL;
	newNode->prev = NULL;

}




void ListPushFrout(LTNode* plist, LTDatatype x)
{
	assert(plist);
	LTNode* newnode = ListBuyNode(x);
	plist->next->prev = newnode;
	newnode->next = plist->next;
	newnode->prev = plist;
	plist->next = newnode;
}


void ListPushBack(LTNode* plist, LTDatatype x)
{
	assert(plist);
	LTNode* newnode = ListBuyNode(x);
	LTNode* prev = plist->prev;
	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = plist;
	plist->prev = newnode;

}



void ListPopFrout(LTNode* plist)

{
	assert(plist->next != plist);
	LTNode* next = plist->next;
	next->next->prev = plist;
	plist->next = next->next;
	free(next);
}




void ListPopBack(LTNode* plist)
{
	assert(plist->prev != plist);
	LTNode* prev = plist->prev;
	plist->prev = prev->prev;
	prev->prev->next = plist;
	free(prev);
}



void ListDestroy(LTNode* plist)
{
	assert(plist);

	LTNode* cur = plist->next;
	while (cur != plist)
	{
		LTNode* next = cur->next;
		free(cur);

		cur = next;
	}

	free(plist);
}


LTNode* ListFind(LTNode* plist, LTDatatype x)
{
	assert(plist);
	LTNode* cur = plist->next;
	while (cur != plist)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
}


void ListInsert(LTNode* pos, LTDatatype x)
{
	assert(pos);
	LTNode* newnode = ListBuyNode(x);
	LTNode* prev = pos->prev;
	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;

}


void ListErase(LTNode* pos)
{
	assert(pos);
	LTNode* prev = pos->prev;
	LTNode* next = pos->next;
	prev->next = next;
	next->prev = prev;
	free(pos);
}